local zhuding = fk.CreateSkill {
  name = "3s__zhuding",
}

Fk:loadTranslationTable{
  ["3s__zhuding"] = "烛鼎",
  [":3s__zhuding"] = "当你摸牌后，你可将至多等量张牌当一张伤害类锦囊牌使用，且目标不能使用与底牌同花色的牌响应；之结算后，你可令因此受伤的角色将一张伤害牌置于你的任意空置装备栏，视为对你使用【杀】，你装备区内的伤害牌视为【虚妄之冕】。",
  ["#zhuding-ask"] = "你可以选择至多%arg张牌当一张伤害类锦囊牌使用",
  ["#3s__zhuding-touse"] = "请选择使用的牌",
  ["#3s__zhuding-slash"] = "烛鼎：你可因此受伤的角色将伤害牌当【虚妄之鼎】置入你的装备区并视为对你使用【杀】",
  ["#3s__zhuding-toput"] = "请选择一张伤害牌置入%src的装备区",
  ["#basha_coronet_skill"] = "虚妄之冕",
}

zhuding:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(zhuding.name) then
      for _, move in ipairs(data) do
        if move.to == player and move.moveReason == fk.ReasonDraw then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      local n = #move.moveInfo
      local all_names = {}
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card:isCommonTrick() and card.is_damage_card and not card.is_derived then
          table.insertIfNeed(all_names, card.name)
        end
      end
      local use = player.room:askToUseVirtualCard(player, {
        name = all_names,
        skill_name = zhuding.name,
        prompt = "#zhuding-ask:::" .. n,
        cancelable = true,
        skip = true,
        card_filter = {
          n = {1, n},
          cards = player:getCardIds("he"),
        },
      })
      if use then
        event:setCostData(self, use)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self)
    room:useCard(use)
  end,
})

zhuding:addEffect(fk.TargetSpecified, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhuding.name) and data.card.skillName == zhuding.name
  end,
  on_use = function(self, event, target, player, data)
    local suits = {}
    for _, id in ipairs(player.room:getSubcardsByRule(data.card)) do
      local card = Fk:getCardById(id)
      table.insertIfNeed(suits, card:getSuitString(true))
    end
    data.extra_data = data.extra_data or {}
    data.extra_data.zhuding_suits = data.extra_data.zhuding_suits or {}
    data.extra_data.zhuding_suits[data.to.id] = suits
  end,
})

zhuding:addEffect(fk.HandleAskForPlayCard, {
  can_refresh = function(self, event, target, player, data)
    if data.eventData and data.eventData.to == player then
      local dat = data.eventData.extra_data
      return dat and dat.zhuding_suits and dat.zhuding_suits[player.id]
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if not data.afterRequest then
      --room:setBanner("zhuding_suits", data.eventData.extra_data.zhuding_suits[player.id])
      room:setPlayerMark(player, "zhuding_suits", data.eventData.extra_data.zhuding_suits[player.id])
    else
      room:setPlayerMark(player, "zhuding_suits", 0)
      --room:setBanner("zhuding_suits", nil)
    end
  end,
})

zhuding:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    local suits = player:getTableMark("zhuding_suits")
    if suits then
      return table.contains(suits, card:getSuitString(true))
    end
  end,
  prohibit_response = function(self, player, card)
    local suits = player:getTableMark("zhuding_suits")
    if suits then
      return table.contains(suits, card:getSuitString(true))
    end
  end,
})

zhuding:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhuding.name) and data.card.skillName == zhuding.name and data.damageDealt
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = zhuding.name,
      prompt = "#3s__zhuding-slash",
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local damaged_targets = {}
    if data.damageDealt then
      for target, damage in pairs(data.damageDealt) do
        if damage > 0 then
            table.insert(damaged_targets, target)
        end
      end
    end
    for _, p in ipairs(damaged_targets) do
      if p:isAlive() and player:isAlive() and player:hasEmptyEquipSlot() then
        local damage_cards = table.filter(p:getCardIds(Player.Hand), function(id) return Fk:getCardById(id).is_damage_card end)
        if #damage_cards > 0 then
          local to_put = room:askToCards(p, {
            min_num = 1,
            max_num = 1,
            skill_name = zhuding.name,
            pattern = tostring(Exppattern{ id = damage_cards }),
            include_equip = false,
            prompt = "#3s__zhuding-toput:"..player.id,
            cancelable = false,
          })
          if #to_put > 0 then
            local emptyslots = {}
            for _, slot in ipairs(player:getAvailableEquipSlots()) do
              if player:hasEmptyEquipSlot(Util.convertSubtypeAndEquipSlot(slot)) then
                table.insert(emptyslots, slot)
              end
            end
            local choice = room:askToChoice(p, {choices = emptyslots, skill_name = zhuding.name})
            local mapper = {
              [Player.WeaponSlot] = "weapon",
              [Player.ArmorSlot] = "armor",
              [Player.OffensiveRideSlot] = "offensive_horse",
              [Player.DefensiveRideSlot] = "defensive_horse",
              [Player.TreasureSlot] = "treasure",
            }
            local coronet = Fk:cloneCard(mapper[choice].."__coronet")
            coronet:addSubcards(to_put)
            room:moveCardIntoEquip(player, coronet, choice, true, player, coronet)
            room:useVirtualCard("lvshi__slash", nil, p, player, zhuding.name, true)
          end
        end
      end
    end
  end,
})

zhuding:addEffect(fk.DrawNCards, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhuding.name)
    and #table.filter(player:getCardIds("e"), function(id) return Fk:getCardById(id, true).is_damage_card end) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local n = #table.filter(player:getCardIds("e"), function(id) return Fk:getCardById(id, true).is_damage_card end) * 2
    data.n = data.n + n
  end,
})

zhuding:addEffect("maxcards", {
  correct_func = function(self, player)
    if player:hasSkill(zhuding.name) then
      return 0 - #table.filter(player:getCardIds("e"), function(id)
        return Fk:getCardById(id, true).is_damage_card
      end)
    end
  end,
})

return zhuding